using Senparc.Weixin.Entities;
using Senparc.Weixin.HttpUtility;
using Senparc.Weixin.QY.AdvancedAPIs.OAuth2;
using Senparc.Weixin.QY.CommonAPIs;
using System;
using System.Collections.Generic;
namespace Senparc.Weixin.QY.AdvancedAPIs
{
	public static class ChatApi
	{
		public static QyJsonResult CreateChat(string accessToken, string chatId, string name, string owner, string[] userlist, int timeOut = 10000)
		{
			string urlFormat = string.Format("https://qyapi.weixin.qq.com/cgi-bin/chat/create?access_token={0}", accessToken);
			var data = new
			{
				chatid = chatId,
				name = name,
				owner = owner,
				userlist = userlist
			};
			return CommonJsonSend.Send<QyJsonResult>(null, urlFormat, data, CommonJsonSendType.POST, timeOut);
		}
		public static GetChatResult GetChat(string accessToken, string chatId)
		{
			string url = string.Format("https://qyapi.weixin.qq.com/cgi-bin/chat/get?access_token={0}&chatid={1}", accessToken, chatId);
			return Get.GetJson<GetChatResult>(url, null);
		}
		public static QyJsonResult UpdateChat(string accessToken, string chatId, string opUser, string name = null, string owner = null, string[] addUserList = null, string[] delUserList = null, int timeOut = 10000)
		{
			string urlFormat = string.Format("https://qyapi.weixin.qq.com/cgi-bin/chat/update?access_token={0}", accessToken);
			var data = new
			{
				chatid = chatId,
				op_user = opUser,
				name = name,
				owner = owner,
				add_user_list = addUserList,
				del_user_list = delUserList
			};
			return CommonJsonSend.Send<QyJsonResult>(null, urlFormat, data, CommonJsonSendType.POST, timeOut);
		}
		public static QyJsonResult QuitChat(string accessToken, string chatId, string opUser, int timeOut = 10000)
		{
			string urlFormat = string.Format("https://qyapi.weixin.qq.com/cgi-bin/chat/quit?access_token={0}", accessToken);
			var data = new
			{
				chatid = chatId,
				op_user = opUser
			};
			return CommonJsonSend.Send<QyJsonResult>(null, urlFormat, data, CommonJsonSendType.POST, timeOut);
		}
		public static QyJsonResult ClearNotify(string accessToken, string opUser, Chat_Type type, string chatIdOrUserId, int timeOut = 10000)
		{
			string urlFormat = string.Format("https://qyapi.weixin.qq.com/cgi-bin/chat/clearnotify?access_token={0}", accessToken);
			var data = new
			{
				op_user = opUser,
				chat = new
				{
					type = type,
					id = chatIdOrUserId
				}
			};
			return CommonJsonSend.Send<QyJsonResult>(null, urlFormat, data, CommonJsonSendType.POST, timeOut);
		}
		public static QyJsonResult SendChatMessage(string accessToken, string sender, Chat_Type type, ChatMsgType msgType, string chatIdOrUserId, string contentOrMediaId, int timeOut = 10000)
		{
			string urlFormat = string.Format("https://qyapi.weixin.qq.com/cgi-bin/chat/send?access_token={0}", accessToken);
			BaseSendChatMessageData data;
			switch (msgType)
			{
			case ChatMsgType.text:
				data = new SendTextMessageData
				{
					receiver = new Receiver
					{
						type = type.ToString(),
						id = chatIdOrUserId
					},
					sender = sender,
					msgtype = msgType.ToString(),
					text = new Chat_Content
					{
						content = contentOrMediaId
					}
				};
				break;
			case ChatMsgType.image:
				data = new SendImageMessageData
				{
					receiver = new Receiver
					{
						type = type.ToString(),
						id = chatIdOrUserId
					},
					sender = sender,
					msgtype = msgType.ToString(),
					image = new Chat_Image
					{
						media_id = contentOrMediaId
					}
				};
				break;
			case ChatMsgType.file:
				data = new SendFileMessageData
				{
					receiver = new Receiver
					{
						type = type.ToString(),
						id = chatIdOrUserId
					},
					sender = sender,
					msgtype = msgType.ToString(),
					file = new Chat_File
					{
						media_id = contentOrMediaId
					}
				};
				break;
			default:
				throw new ArgumentOutOfRangeException("msgType");
			}
			return CommonJsonSend.Send<QyJsonResult>(null, urlFormat, data, CommonJsonSendType.POST, timeOut);
		}
		public static SetMuteResult SetMute(string accessToken, List<UserMute> userMuteList, int timeOut = 10000)
		{
			string urlFormat = string.Format("https://qyapi.weixin.qq.com/cgi-bin/chat/setmute?access_token={0}", accessToken);
			var data = new
			{
				user_mute_list = userMuteList
			};
			return CommonJsonSend.Send<SetMuteResult>(null, urlFormat, data, CommonJsonSendType.POST, timeOut);
		}
	}
}
